package cn.rokhdelar.umbrsapi.management.controller;

import cn.rokhdelar.umbrsapi.management.entity.SysRole;
import cn.rokhdelar.umbrsapi.management.service.SysRoleService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "management/roles")
public class SysRoleController {
    private final SysRoleService sysRoleService;

    public SysRoleController(SysRoleService sysRoleService) {
        this.sysRoleService = sysRoleService;
    }

    @GetMapping
    public Page<SysRole> list(@RequestParam(value = "name", defaultValue = "") String name,
                              @RequestParam(value = "current", defaultValue = "0") Integer current,
                              @RequestParam(value = "size", defaultValue = "20") Integer size) {
        PageRequest pageRequest = PageRequest.of(current, size);
        return sysRoleService.list(name, pageRequest);
    }

    @GetMapping(value = "/{id}")
    public SysRole get(@PathVariable Long id) {
        return sysRoleService.get(id);
    }

    @PostMapping
    public SysRole create(@RequestBody SysRole sysRole) {
        return sysRoleService.save(sysRole);
    }

    @PatchMapping(value = "/{id}")
    public SysRole update(@PathVariable Long id, @RequestBody SysRole sysRole) {
        return sysRoleService.save(sysRole);
    }

    @DeleteMapping(value = "/{id}")
    public void delete(@PathVariable Long id) {
        sysRoleService.delete(id);
    }
    
}
